package mainView;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;

import javax.swing.JMenu;

import view.BlockPanel;
import view.BlockedProductPanel;
import view.ErrorPanel;
import view.FridayPanel;
import view.RegisterDeliveryPanel;
import view.RegisterPalletPanel;
import view.SearchDeliveryPanel;
import view.SearchPalletTime;
import view.SearchPalletsPanel;

import model.Database;

import control.OrderControl;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductionView extends JFrame{
	private OrderControl oc;
	private SearchPalletsPanel barcodeSearch;
	private RegisterDeliveryPanel dr;
	private SearchDeliveryPanel dp;
	private RegisterPalletPanel rp;
	private FridayPanel fp;
	private JPanel centerPanel;
	private BlockPanel bp;
	private BlockedProductPanel bpp;
	private CardLayout layout;
	private SearchPalletTime sptp;
	public static final String DELIVERY_REGISTER = "DELIVERY REGISTER";
	public static final String PALLETS_REGISTER = "PALLETS REGISTER";
	public static final String BARCODE_SEARCH = "CUSTOMER SEARCH";
	public static final String DELIVERY_SEARCH = "DELIVERY SEARCH";
	public static final String BLOCK_PALLET = "BLOCK PALLET";
	public static final String FRIDAY_PANEL = "FRIDAY PANEL";
	public static final String BLOCKED_PRODUCT = "BLOCKED PRODUCT";
	public static final String PALLET_TIME = "PALLET TIME";
	
	public ProductionView(OrderControl oc) {
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.oc = oc;

		
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new exitAction());
		mnFile.add(mntmExit);
		
		JMenu mnSearch = new JMenu("Search");
		menuBar.add(mnSearch);
		
		JMenuItem mntmCustomers = new JMenuItem("Pallets");
		mntmCustomers.addActionListener(new barcodeSearch());
		mnSearch.add(mntmCustomers);
		
		JMenuItem mntmDeliveries = new JMenuItem("Deliveries");
		mntmDeliveries.addActionListener(new deliveriesSearch());
		mnSearch.add(mntmDeliveries);
		
		JMenuItem mntmBlockedProducts = new JMenuItem("Blocked products");
		mntmBlockedProducts.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				layout.show(centerPanel, BLOCKED_PRODUCT);
			}
		});
		mnSearch.add(mntmBlockedProducts);
		
		JMenuItem mntmPalletstime = new JMenuItem("Pallets (time)");
		mntmPalletstime.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				layout.show(centerPanel, PALLET_TIME);
			}
		});
		mnSearch.add(mntmPalletstime);
		
		
		
		JMenu mnRegister = new JMenu("Register");
		menuBar.add(mnRegister);
		
		JMenuItem mntmPallets = new JMenuItem("Pallets");
		mntmPallets.addActionListener(new registerPallets());
		mnRegister.add(mntmPallets);
		
		JMenuItem mntmDeliveries_1 = new JMenuItem("Deliveries");
		mntmDeliveries_1.addActionListener(new registerDelivery());
		mnRegister.add(mntmDeliveries_1);
		
		JMenu mnBlock = new JMenu("Block");
		menuBar.add(mnBlock);
		
		JMenuItem mntmBlockPallet = new JMenuItem("Block pallet");
		mntmBlockPallet.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				layout.show(centerPanel, BLOCK_PALLET);
			}
		});
		mnBlock.add(mntmBlockPallet);
		
		centerPanel = new JPanel(new CardLayout());
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		
		ErrorPanel errorPanel = new ErrorPanel(oc);
		errorPanel.setPreferredSize(new Dimension(this.getWidth(),50)); //set size for error panel
		getContentPane().add(errorPanel, BorderLayout.SOUTH);
		Database.getInstance().addObserver(errorPanel); //Observer! ;)
		
		layout = (CardLayout) centerPanel.getLayout();
		
		dr = new RegisterDeliveryPanel(oc);
		centerPanel.add(dr, DELIVERY_REGISTER);
		rp = new RegisterPalletPanel(oc);
		centerPanel.add(rp, PALLETS_REGISTER);
		barcodeSearch = new SearchPalletsPanel(oc);
		centerPanel.add(barcodeSearch, BARCODE_SEARCH);
		dp = new SearchDeliveryPanel(oc);
		centerPanel.add(dp, DELIVERY_SEARCH);	
		bp = new BlockPanel(oc);
		centerPanel.add(bp, BLOCK_PALLET);
		fp = new FridayPanel();
		centerPanel.add(fp, FRIDAY_PANEL);
		bpp = new BlockedProductPanel(oc);
		centerPanel.add(bpp, BLOCKED_PRODUCT);
		sptp = new SearchPalletTime(oc);
		centerPanel.add(sptp, PALLET_TIME);
		
		//DEAFULT???????? DECIDE; MAYBY A IS IT FRIDAY????!
		layout.show(centerPanel, FRIDAY_PANEL);
		centerPanel.setPreferredSize(new Dimension(750, 450));
		
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setLocationRelativeTo(getOwner());
		
		oc.initiateSystem();
	
	}
	
	private class exitAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			oc.exitSystem();			
		}		
	}
	
	private class barcodeSearch implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			layout.show(centerPanel, BARCODE_SEARCH);
		}		
	}
	
	private class deliveriesSearch implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			layout.show(centerPanel, DELIVERY_SEARCH);
		}		
	}
	
	private class registerPallets implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			layout.show(centerPanel, PALLETS_REGISTER);
		}		
	}
	
	private class registerDelivery implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			layout.show(centerPanel, DELIVERY_REGISTER);
		}		
	}
}
